package gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Marija Prokopijevic
 *
 */

public class RucniUnosPanel extends JPanel{
	private JTextField[] brojevi;
	private int velicinaMemorije;
	private JPanel panelZaDugmice;
	private JPanel panelZaBrojeve;
	private JButton buttonOK;
	private JButton buttonPonisti;
	private JButton buttonOdustani;
	private static ArrayList<Integer> sadrzajMemorije; 
	private static final long serialVersionUID = 1L;
	public RucniUnosPanel(int velicinaMemorije)
	{
		this.velicinaMemorije = velicinaMemorije;
		
		postaviLayout();
		postaviGUI();
		
		sadrzajMemorije = new ArrayList<Integer>();
	}
	
	private void postaviGUI(){
		
		panelZaDugmice = new JPanel();
		buttonOK = new JButton("OK");
		buttonOK.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				RucniUnosPanel.this.setVisible(false);
				
			}
			
		});
		buttonPonisti = new JButton("Ponisti");
		buttonPonisti.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				for(int i = 0; i < brojevi.length; i++)
				{
					brojevi[i].setText("");
				}
			}
			
		});
		buttonOdustani = new JButton("Odustani");
		buttonOdustani.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {		
				RucniUnosPanel.this.setVisible(false);
			}
			
		});
		
		panelZaDugmice.add(buttonOK);
		
		panelZaDugmice.add(buttonPonisti);
		
		panelZaDugmice.add(buttonOdustani);
		this.add(panelZaDugmice);
	}
	private void postaviLayout() {
		boolean neparno = false;
		panelZaBrojeve = new JPanel();
		if(velicinaMemorije % 2 == 0)
		{
			this.setLayout(new GridLayout(velicinaMemorije/2, velicinaMemorije/2));
			brojevi = new JTextField[velicinaMemorije];
		}
		else
		{
			neparno = true;
			int lS = velicinaMemorije + 1;
			this.setLayout(new GridLayout(lS/2, lS/2));
			brojevi = new JTextField[lS];
		}
		for(int i = 0; i < brojevi.length; i++)
		{
			brojevi[i] = new JTextField(3);
			panelZaBrojeve.add(brojevi[i]);
		}
		if(neparno)
			panelZaBrojeve.add(new JLabel(""));
		this.add(panelZaBrojeve);
	}

	public void setSadrzajMemorije(JTextField[] fieldovi) {
		for(int i = 0; i < fieldovi.length; i++)
		{
			sadrzajMemorije.add(Integer.parseInt(fieldovi[i].getText()));
		}
	}

	public static ArrayList<Integer> getSadrzajMemorije() {
		return sadrzajMemorije;
	}
	
	
	
}
